
/*
'===============================================================================
'  HNG.Portal.BL.TblLienKetWebManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Text;

namespace HNG.Portal.BLL
{
	public class TblLienKetWebManager : TblLienKetWebManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblLienKetWebManager()
		{
			// Nothing for now.
		}

        public DataTable be_SelectToGriView(TblLienKetWebEntity _TblLienKetWebEntity)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            StringBuilder sbSql = new StringBuilder();
            SqlParameter[] _SqlParameter = new SqlParameter[2];
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblLienKetWebEntity.SortExpression) ? "[ID]" : _TblLienKetWebEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblLienKetWebEntity.SortDirection) ? " DESC" : _TblLienKetWebEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[TenLienKet],").Append("\n");
                sbSql.Append("  ch.[Mota],").Append("\n");
                sbSql.Append("  ch.[Url],").Append("\n");
                sbSql.Append("  ch.[TrangThai]").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblLienKetWeb ch").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblLienKetWebEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblLienKetWebEntity.Id);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");
                }
                string strCon = _TblLienKetWebEntity.Condition;
                if (!string.IsNullOrEmpty(strCon))
                {
                    sbSql.AppendFormat("  AND (ch.[ID] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenLienKet] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Mota] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Url] LIKE '%{0}%'", strCon).Append("\n");

                    sbSql.AppendFormat("  OR ch.[ID] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenLienKet] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Url] LIKE N'%{0}%'", strCon).Append("\n");

                    sbSql.Append(")").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblLienKetWebEntity.Status))
                {
                    _SqlParameter[1] = new SqlParameter("@TrangThai", _TblLienKetWebEntity.TrangThai);
                    sbSql.Append("  AND ch.[TrangThai] Like @TrangThai").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblLienKetWebEntity.StartIndex, _TblLienKetWebEntity.EndIndex).Append("\n");
            }
            catch { }


            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString(), _SqlParameter);
        }
	}
}
